package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.PermissionService;
import cn.wolfcode.crm.utils.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/permission/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {


        try {
            permissionService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            return new JsonResult(false, "删除失败");
        }

        return new JsonResult(true, "删除成功");

    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        if (id != null) {
            Permission permission = permissionService.selectByPrimaryKey(id);
            model.addAttribute("permission", permission);
        }
        return "/permission/input";

    }

    @RequestMapping("/save")
    public String save(Permission permission) {

        permissionService.insert(permission);

        return "redirect:/permission/list.do";

    }


    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload() {
        try {
            permissionService.reload();
        } catch (Exception e) {
            return new JsonResult(false, "权限加载失败");
        }
        return new JsonResult(true, "权限加载成功");

    }


}
